package com.wf.course.service.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.utils.page.PageBean;
import com.wf.course.dao.ICourseInfoMapper;
import com.wf.course.model.course.CourseInfo;
import com.wf.course.model.course.Speaker;
import com.wf.course.service.ICourseInfoService;
import com.wf.course.web.Constants;
import com.wf.course.web.OSSUtil;

@Service
@Transactional(rollbackFor=Exception.class)
public class ICourseInfoServiceImpl implements ICourseInfoService{
	
	@Autowired
	private ICourseInfoMapper mapper;

	@Override
	public void indexInfo(PageBean<CourseInfo> pageBean) {
		// TODO Auto-generated method stub
		pageBean.setResults(this.mapper.queryList(pageBean));
		pageBean.setTotalCount(this.mapper.getTotalCount(pageBean));
	}

	@Override
	public void saveOrUpdateCourseInfo(CourseInfo courseInfo,MultipartFile courseFile) {
		// TODO Auto-generated method stub
		
		//添加
		if(courseInfo.getId()==null){
			//创建时间
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			courseInfo.setCreateTime(df.format(new Date()));
			if (courseFile!=null && !courseFile.isEmpty()) {
				String courseImgUrl = "";
				try {
					courseImgUrl = OSSUtil.addFile(courseFile, "wf_course/imgPath", Constants.IGM_BUCKET);
				} catch (IOException e) {
					e.printStackTrace();
				}
				courseInfo.setCourseImgUrl(courseImgUrl);
			}
			
			this.mapper.saveCourseInfo(courseInfo);
		}else{
		 //修改

			if (courseFile!=null && courseFile.getSize() != 0) {
				CourseInfo cours = mapper.queryCourseInfoById(courseInfo.getId());
				try{
					if(StringUtils.isNotBlank(cours.getCourseImgUrl())){
						// 删除原来图片
						OSSUtil.delete(cours.getCourseImgUrl(), Constants.IGM_BUCKET);
					}
				}catch(Exception e){
					System.out.println(e.getMessage());
				}
				// 重新上传
				// 上传
				String courseImgUrl = "";
				try {
					courseImgUrl = OSSUtil.addFile(courseFile, "wf_course/imgPath", Constants.IGM_BUCKET);
				} catch (IOException e) {
					e.printStackTrace();
				}
				courseInfo.setCourseImgUrl(courseImgUrl);
			}
			
		  this.mapper.updateCourseInfo(courseInfo);
			
		}
		
	}

	@Override
	public CourseInfo queryCourseInfoById(Integer id) {
		// TODO Auto-generated method stub
		return this.mapper.queryCourseInfoById(id);
	}

	@Override
	public void deleteById(Integer id) {
		// TODO Auto-generated method stub
		this.mapper.deleteById(id);
	}

	@Override
	public List<Speaker> querySpeaker(){
		return this.mapper.querySpeaker();

	}
	
	@Override
	public List<CourseInfo> queryCourses(){
		return this.mapper.queryCourses();
	}

	@Override
	public void updateCourseSaleNum(CourseInfo courseInfo) {
		this.mapper.updateCourseSaleNum(courseInfo);
	}

	@Override
	public void CourseInfoTop(Integer id) {
		// TODO Auto-generated method stub
		//获取所有课程信息列表
		List<CourseInfo> list = mapper.queryCourses();
		int order = 2;
		for (int i = 0; i < list.size(); i++) {
			if (id != list.get(i).getId()) {
				list.get(i).setOrderNum(order);
				order ++;
			}else {
				list.get(i).setOrderNum(1);
			}
		}
		mapper.updateOrderNumBatch(list);
	}


}
